#include "tetris_adapter.h"
#include "tetris_type.h"
#include "bmp.h"
#include "oled.h"
#include <stdint.h>
#include "systick.h"

static uint8_t BACKGROUD[PLAYGROUND_HEIGHT][PLAYGROUND_WIDTH] = {
    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}};
extern uint8_t SHAPE_L_0[3][3];
extern uint8_t SHAPE_L_1[3][3];
extern uint8_t SHAPE_L_2[3][3];
extern uint8_t SHAPE_L_3[3][3];
extern uint8_t SHAPE_LS[4][3][3];

void tetris_adapter_init()
{
    i2c_soft_config();
    OLED_Init();
}

void draw_block(uint8_t x, uint8_t y, uint8_t bit)
{
    size_t w, h;
    for (h = y * BLOCK_HEIGHT; h < (y + 1) * BLOCK_HEIGHT; h++)
    {
        for (w = x * BLOCK_HEIGHT; w < BLOCK_WIDTH * (x + 1); w++)
        {
            OLED_DrawPoint(w, h, bit);
        }
    }
}

void draw_bg()
{
    // OLED_Clear();
    size_t w, h;
    for (h = 0; h < PLAYGROUND_HEIGHT; h++)
    {
        for (w = 0; w < PLAYGROUND_WIDTH; w++)
        {
            draw_block(w, h, BACKGROUD[h][w]);
        }
    }

    // OLED_ShowString(0, 0, dateStr, 8, 1); // 6*8 “ABC”
    OLED_Refresh();
}

void draw_shape(Shape shape)
{
    // shape.shape
    uint8_t pattern[3][3] = {{0, 1, 0}, {0, 1, 0}, {0, 1, 1}}; // SHAPE_L_0; // SHAPE_LS[shape.shape_pattern];
    size_t col, row;
    for (row = 0; row < 3; row++)
    {
        for (col = 0; col < 3; col++)
        {
            draw_block(shape.x + col, shape.y + row, pattern[row][col]);
        }
    }

    // OLED_ShowString(0, 0, dateStr, 8, 1); // 6*8 “ABC”
    OLED_Refresh();
}

void draw_frame_clean()
{
    // OLED_Clear();
}

void tetris_frame_delay(uint32_t ms)
{
    delay_1ms(ms);
}
